package com.sservice.service.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sservice.commonBusiness.e.DataR;
import com.sservice.commonBusiness.e.Statistic;
import com.sservice.commonBusiness.service.StatisticService;
import com.sservice.model.dao.UserWxMapper;
import com.sservice.model.entity.SaleLevel;
import com.sservice.model.entity.UserWx;
import com.sservice.model.vo.UserWxVo;
import com.sservice.service.service.ISaleLevelService;
import com.sservice.service.service.IUserWxService;
import com.sservice.service.service.Redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by suweiming on 2017/12/20.
 */


@Service
public class UserWxServiceImpl implements IUserWxService {

    @Autowired
    private UserWxMapper userWxDao;
    @Autowired
    private Redis redis;
    @Autowired
    StatisticService statisticService;

    /**
     * view -> factor1
     * favor -> factor2
     * guidance -> factor3
     * call1 -> factor4
     *
     * @param userId
     * @param factor1
     * @param factor2
     * @param factor3
     * @param factor4
     */
    @Override
    public void setFactors(Integer userId, Integer factor1, Integer factor2, Integer factor3, Integer factor4) {

        if (factor1 != null) {
            statisticService.plus(String.valueOf(userId),
1
//                    "uw-factor"

                    , "view", factor1);
        }
        if (factor2 != null) {
            statisticService.plus(String.valueOf(userId),
                    1
                   // "uw-factor"
                    , "favor", factor2);
        }
        if (factor3 != null) {
            statisticService.plus(String.valueOf(userId),
                   // "uw-factor"
                    1
                    , "guidance", factor3);
        }
        if (factor4 != null) {
            statisticService.plus(String.valueOf(userId),
                    1
                   // "uw-factor"
                    , "call1", factor4);
        }
        this.upgradeLevel(userId);
    }

    @Autowired
    ISaleLevelService iSaleLevelService;

    @Override
    public void upgradeLevel(Integer userId) {
        Statistic statistic = statisticService.selectBy(String.valueOf(userId), 1);
        List<SaleLevel> list = iSaleLevelService.selectAll();
        int i = 1;
        for (int level = 0; level < list.size(); level++) {
            SaleLevel saleLevel = list.get(level);
            saleLevel.getFactor1();
            if (
                    statistic.getView().compareTo(BigDecimal.valueOf(saleLevel.getFactor1())) != -1
                            && statistic.getFavor().compareTo(BigDecimal.valueOf(saleLevel.getFactor2())) != -1
                            && statistic.getGuidance().compareTo(BigDecimal.valueOf(saleLevel.getFactor3())) != -1
                            && statistic.getCall1().compareTo(BigDecimal.valueOf(saleLevel.getFactor4())) != -1
                    ) {
                i++;
            } else {
                break;
            }
        }
        UserWx userWx = new UserWx();
        userWx.setLevel(i);
        userWx.setId(userId);
        userWxDao.updateByPrimaryKeySelective(userWx);
    }

    public UserWx getLogined(String skey) {
        JSONObject obj = redis.getObj(skey);
        if (obj != null) {
            UserWx salePerson = new UserWx();
            salePerson.setNickName(obj.getString("nickName"));
            salePerson.setHeadimgurl(obj.getString("headimgurl"));
            salePerson.setId(obj.getInteger("id"));
            salePerson.setPoints(obj.getBigDecimal("points"));
            salePerson.setPointsNow(obj.getBigDecimal("pointsNow"));
            salePerson.setProfile(obj.getString("profile"));
            salePerson.setPhone(obj.getString("phone"));
            salePerson.setOpenid(obj.getString("openid"));
            salePerson.setCountry(obj.getString("country"));
            salePerson.setCity(obj.getString("city"));
            salePerson.setUnitid(obj.getString("unitid"));
            return salePerson;
        }
        ;
        return null;
    }

    @Override
    public List<UserWxVo> queryCommonByPage(UserWxVo record) {
        return userWxDao.queryCommonByPage(record);
    }

    @Override
    public int countCommonByPage(UserWxVo record) {
        return userWxDao.countCommonByPage(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userWxDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(UserWx record) {
        return userWxDao.insert(record);
    }

    @Override
    public int insertSelective(UserWx record) {
        return userWxDao.insertSelective(record);
    }

    @Override
    public UserWx selectByPrimaryKey(Integer id) {
        return userWxDao.selectByPrimaryKey(id);
    }

    @Override
    public UserWx selectByOpenid(String openid) {
        return userWxDao.selectByOpenId(openid);
    }

    @Override
    public UserWx selectByUnionid(UserWx record) {
        return userWxDao.selectByUnionid(record);
    }

    @Override
    public int updateByPrimaryKeySelective(UserWx record) {
        return userWxDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(UserWx record) {
        return userWxDao.updateByPrimaryKey(record);
    }

    @Override
    public List<UserWxVo> queryWithDr(DataR record) {
        return userWxDao.queryWithDr(record);
    }
}
